<?php
namespace admin\Controller;
use Think\Controller;
class LoginController extends Controller {


    public function index(){
        $this->display();
    }

    /*****生成验证码*****/
    public function verify(){
        //验证码参数
        $config=array(
            'imageW'=>200,
            'imageH'=>80,
            'length'=>4,
            'useNoise'=>false,
        );

        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }


    /******登陆方法*****/
    public function login(){
        //实例化administrator模型
        $db=M('administrators');

        //接收表单传入的参数
        $admin['administrator_name|administrator_email']=I('post.admin_name_email');
        $admin['administrator_password']=I('post.admin_password');
        //获取表单输入的验证码
        $verify=I('post.verify');
        //判断是否为空
        if(empty($admin['administrator_name|administrator_email'])||empty($admin['administrator_password'])){
            $this->error("账号或密码不能为空");
        }else{
            //检测验证码是否正确,check_verify函数在../Application/Common/Common/function.php中
            if(!check_verify(strtolower($verify))){
                $this->error("验证码错误!",U('index'),1);
            }else{
                //进行数据查询
                $select_result=$db->where($admin)->find();

                //判断查询结果
                if($select_result){
                    $_SESSION['islogin']=1;
                    $_SESSION['administrator_id']=$select_result['administrator_id'];
                    $_SESSION['administrator_name']=$select_result['administrator_name'];
                    $result=json_encode($_SESSION);
                    $this->success("登陆成功！");
                    return $result;
                }else{
                    $_SESSION['islogin']=0;
                    $_SESSION['administrator_id']=null;
                    $_SESSION['administrator_name']=null;
                    $result=json_encode($_SESSION);
                    $this->success("登陆失败，账号或密码错误");
                    return $result;
                }
            }
        }
    }


    public function RegPost()
    {
        $data['user_name']=$_POST['user_name'];
        $data['user_password']=$_POST['user_password'];
        $data['reuser_password']=$_POST['reuser_password'];
        $data['user_eamil']=$_POST['user_eamil'];

        $Form = D('users');
        if($Form->create())
        {
            if($Form->add($data))
            {
                $this->success('注册成功！','login');
            }
            else
            {
                $this->error('注册失败！');
            }
        }
        else
        {
            exit($Form->getError());
        }
    }
}